﻿

using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;

namespace PowerStation.AppCode
{
    /// <summary>
    /// 
    /// </summary>
    public class ArrayInputAttribute : Attribute, IActionFilter
    {
        private readonly string _parameternames;


        /// <summary>
        /// 
        /// </summary>
        /// <param name="parameternames"></param>
        public ArrayInputAttribute(string parameternames)
        {
            _parameternames = parameternames;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="System.NotImplementedException"></exception>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            var ActionArgument = context.ActionArguments.Where(x => x.Value is RequestModel.RequestMultipleSearchModel).ToList();
            if (ActionArgument.Count == 0)
            {
                return;
            }
            RequestModel.RequestMultipleSearchModel m = (RequestModel.RequestMultipleSearchModel)ActionArgument[0].Value;
            if (m.searchItem != null && m.searchItem.Any())
            {
                return;
            }
            List<RequestModel.RequestSingleSearchModel> lst = new List<RequestModel.RequestSingleSearchModel>();
            var items = context.HttpContext.Request.Query[this._parameternames];
            foreach (var item in items)
            {
                var itemModel = System.Text.Json.JsonSerializer.Deserialize<RequestModel.RequestSingleSearchModel>(item);
                lst.Add(itemModel);
            }
            m.searchItem = lst;
        }
    }
}
